package com.lysj.bhp.system.service;

import com.lysj.bhp.system.domain.IpReord;
import com.lysj.bhp.system.repository.IpRecordRepository;
import com.lysj.bhp.util.web.ParamUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class IpRecordService {
    public final static long RECORD_TIME = 1000 * 60; //60秒
    public final static long BLOCK_TIME = 1000 * 60 * 15; //15分钟
    public final static int MAX_RECORD = 15;

    @Resource
    IpRecordRepository ipRecordRepository;

    public boolean verifyIp(String ip) {
        if (ParamUtil.isBlack(ip)) {
            return false;

        }
        IpReord ipReord = ipRecordRepository.findOne(ip);
        if (ipReord == null) { // 新来的Ip,创建记录
            ipReord = new IpReord();
            ipReord.setIp(ip);
            ipReord.setTimes(1);
            ipReord.setStartRecordTime(System.currentTimeMillis());
            ipReord.setEndRecordTime(System.currentTimeMillis() + RECORD_TIME); // 监控时间
            ipReord.setBlockEndTime(Long.MAX_VALUE);
            ipReord.setStatus(1); // 正常
            ipRecordRepository.save(ipReord);
            System.out.println("New Add IP :-> true");
            return true;
        }

        if (ipReord.getStatus() == -400) { // 永久封禁
            System.out.println("Blacklist IP :-> false");
            return false;
        }

        if (ipReord.getStatus() == -100) { // 临时封禁
            if (System.currentTimeMillis() < ipReord.getBlockEndTime()) { // 还在封禁期内
                System.out.println("Bolck temple :-> false");
                return false;
            } else { //封禁结束,开始正常记录
                ipReord.setStartRecordTime(System.currentTimeMillis());
                ipReord.setEndRecordTime(System.currentTimeMillis() + RECORD_TIME); // 监控时间
                ipReord.setBlockEndTime(Long.MAX_VALUE);
                ipReord.setTimes(0);
                ipReord.setStatus(1); // 正常
                ipRecordRepository.save(ipReord);
                System.out.println("Block end,Start a new record cycle:-> true");
                return true;
            }
        }
        if (ipReord.getStatus() == 1) { //正常状态
            if (System.currentTimeMillis() < ipReord.getEndRecordTime()) { // 循环周期内,超过规定的次数,暂时封禁
                if (ipReord.getTimes() > MAX_RECORD) {
                    ipReord.setBlockEndTime(System.currentTimeMillis() + BLOCK_TIME);
                    ipReord.setStatus(-100);
                    ipRecordRepository.save(ipReord);
                    System.out.println("In record cycle -> More than MAX -> set Block:-> false");
                    return false;
                } else {
                    ipReord.setTimes(ipReord.getTimes() + 1);
                    ipRecordRepository.save(ipReord);
                    System.out.println("In record cycle ->not more than MAX :-> true:-> times:"+ipReord.getTimes());
                    return true;// 循环周期内,不超过规定的次数,放行
                }
            } else {// 代表一次记录完成,开始下一次记录
                ipReord.setStartRecordTime(System.currentTimeMillis());
                ipReord.setEndRecordTime(System.currentTimeMillis() + RECORD_TIME); // 监控时间
                ipReord.setBlockEndTime(Long.MAX_VALUE);
                ipReord.setTimes(0);
                ipReord.setStatus(1); // 正常
                ipRecordRepository.save(ipReord);
                System.out.println("Block temple timeout -> start a new record cycle :-> true");
                return true;
            }
        }
        return false;
    }
}
